<!--

/*
* Copyright (C) 2005-2013 University of Sydney
*
* Licensed under the GNU License, Version 3.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/gpl-3.0.txt
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

/**
* brief description of file
*
* @author      Tom Murtagh
* @author      Kim Jackson
* @author      Ian Johnson   <ian.johnson@sydney.edu.au>
* @author      Stephen White   <stephen.white@sydney.edu.au>
* @author      Artem Osmakov   <artem.osmakov@sydney.edu.au>
* @copyright   (C) 2005-2013 University of Sydney
* @link        http://Sydney.edu.au/Heurist
* @version     3.1.0
* @license     http://www.gnu.org/licenses/gpl-3.0.txt GNU License 3.0
* @package     Heurist academic knowledge management system
* @subpackage  !!!subpackagename for file such as Administration, Search, Edit, Application, Library
*/

-->


<html>
	<head>
		<title>Import Records from CSV (comma-separated values)</title>

        <meta http-equiv="content-type" content="text/html; charset=utf-8">
		<script src="../../common/php/loadHAPI.php"></script>

		<script>
			if (!(HAPI && HAPI.CurrentUser.isLoggedIn())){
				window.location = HeuristBaseURL + "common/connect/login.php?db="+HAPI.database;//saw FIXME: instance code here
			}
		</script>

		<script src="../../hapi/js/goi.js"></script>

		<script src="../../external/jquery/jquery.js"></script>
		<script src="../../common/js/temporalObjectLibrary.js"></script>
		<script src="importRecordsFromDelimited.js"></script>

		<style>
			#csv-textarea {
				width: 100%;
				height: 400px;
				border: 1px solid #6A7C99;
			}
			#csv-separator { width: 70px; }
			#val-separator, #csv-terminator { width: 30px; }
			table { border-collapse: collapse; }
			td { width: 100px; border: 1px solid gray; padding: 2px; }
			td select { width: 100px; }
			td.error { color: red; }
			#col-select-row td { border: none; padding: 1px 5px 5px 0px; vertical-align: top; }
			.invalidRecord { background-color: FFCBCB;}
			.duplicateRecord { background-color: CBCBFF;}
			.invalidInput { font-weight: bold; color: red;}
			.errorMsg { font-size: 90%; color: red; line-height:90%}
			.required { font-weight: 900;}
			.message {font-size: 130%; font-weight: bold; color: black;}

			.header-steps{
				display:inline-table;
				padding-left:20px;
				color:#aaffaa;
			 	background-image: url('../../common/images/tright.gif');
			 	background-repeat: no-repeat;
			 	background-position: 0 center;
			}
			.btn_nextstep{
				padding-right: 20px !important;
			 	background-image: url('../../common/images/tright.gif');
			 	background-repeat: no-repeat;
			 	background-position: right center;
			}
			.header-div{
				height:155px;
				width:100%;
				border-bottom:
				black 2px solid;
			}
			div#div-progress {
				background-image: url(../../common/images/loading-animation-white.gif);
				background-repeat: no-repeat;
				background-position:center center;
			}
			.header-steps.current{
			 	font-weight: bold;
			 	color:#000;
			}
			.header-steps.link{
				cursor:pointer;
			  	text-decoration: underline;
			  	color:#000;
			}
			.hidden{
				height:0px;
				display:none;
			}
			.step-info{
				font-style: italic;
				padding:5px;
			}
			.step-commands{
				padding:5px;
				width:80%;
			}
			.step-notes{
			 	position: absolute;
			 	top:45px;
			 	right:5px;
				height:120px;
				width:450px;
			}
			li{
				padding-top:5px;
			}

		</style>
	<link rel=stylesheet href="../../common/css/global.css" media="all">
	<link rel=stylesheet href="../../common/css/admin.css" media="all">
	</head>

	<body class="popup">

		<script src="../../common/js/utilsLoad.js"></script>
		<!-- script src="../../common/php/loadCommonInfo.php"></script -->
		<script src="../../common/php/getMagicNumbers.php"></script>
		<script src="../../common/js/utilsUI.js"></script>
		<script src="../../common/php/displayPreferences.php"></script>

		<h2></h2>

		<div id="div-progress" class="header-div hidden">
			&nbsp;
		</div>

		<div  id="div-steps" class="header-div">

			<div style="padding: 2px;">
				<div id="mstep1" class="header-steps current" title="Start over">STEP 1</div>
				<div id="mstep2" class="header-steps" title="Select record type and map fields">STEP 2</div>
				<div id="mstep3" class="header-steps" title="Prepare and fix data issues">STEP 3</div>
				<div id="mstep4" class="header-steps" title="Save into database">FINISH</div>
			</div>

			<!-- commands -->
			<div id="astep1">
				<div class="step-info">Paste CSV or tab delimited data into the box below and click Analyse.</div>
				<div class="step-commands">
					Field separator: <select id=csv-separator><option value="," selected>comma</option><option value="\t">tab</option></select>&nbsp;&nbsp;&nbsp;
					Multi-value separator: <select id=val-separator><option selected value="|">|</option><option value=",">,</option><option value=":">:</option><option value=";">;</option></select>&nbsp;&nbsp;&nbsp;
					Line separator: <input id=csv-terminator value="\n">&nbsp;&nbsp;&nbsp;
					Quote: <select id=csv-quote><option selected value='"'>"</option><option value="'">'</option></select><br/><br/>
					Header lines: <input type="checkbox" id=csv-header />
					<br/><br/><input type="button" value="analyse" class="btn_nextstep" onClick="FlexImport.analyseCSV();">
				</div>
				<div class="step-notes help prompt">
					<div style="padding-left: 50px;">Notes:</div>
				<ul>
					<li>Only one record type can be imported at a time. It is best to break large files into blocks of a couple of thousand lines</li>
					<li>geographic data is accepted in OpenGIS WKT format, e.g. POINT(x y). See <a href="http://en.wikipedia.org/wiki/Well-known_text" target="_blank">this Wikipedia article</a> for more details.</li>
					<li>To specify multiple values for a repeatable field, separate the values with a | (pipe) symbol within the field, eg. 1,2,"3|4",5</li>
				</ul>
				</div>
			</div>
			<div id="astep2" class="hidden">
				<div class="step-info">Select record type or select saved mapping. Then select column assignment and click Prepare Records.</div>
				<div class="step-commands">
					<div id="rec-type-select-div"></div>
					<br/>
					<div>Duplicate records:&nbsp;<input type="radio" name="rg_duprec" value="0" checked="checked"/>&nbsp;ignore&nbsp;
												 <input type="radio" name="rg_duprec" value="1"/>&nbsp;create record&nbsp;
												 <input type="radio" name="rg_duprec" value="2"/>&nbsp;ask individually</div><br/>
					<input type="button" value="Save field mapping" onClick="FlexImport.saveMappings();">&nbsp;&nbsp;
					<input type="button" value="Prepare records" class="btn_nextstep" onClick="FlexImport.loadReferencedRecords();">
				</div>
				<div class="step-notes help prompt">
					<div style="padding-left: 50px;">Notes:</div>
				<ul>
					<li>In order to normalise the data, eg. to extract a list of persons (entities) as records and then point to these person records rather than including names repetitively in the main data records, start by importing only those fields relating to the entities to be normalised. After import, the data will be redisplayed with the ID numbers for the extracted records, which can be used as a pointer field in the subsequent import of the remaining columns of data.</li>
					<li>You needn't assign all the columns - unassigned columns will be ignored.</li>
					<li>Duplicated records will be treated as you specified.</li>
				</ul>
				</div>
			</div>
			<div id="astep3" class="hidden">
				<div id="step3-info" class="step-info"></div>
				<div class="step-commands">
					<div id="prepare-info-div"></div>
					<br/><br/>
					<span id="btn_correct" style="display:none;"><input type="button"
							style="display:none;" value="Correct the data" onClick="FlexImport.createColumnSelectors();"></span>
					<span id="btn_prepare" style="display:none;"><input type="button"
							value="Prepare records" onClick="FlexImport.loadReferencedRecords();"></span>
					<span id="btn_save" style="display:none;"><input type="button"
							value="Save records" class="btn_nextstep" onClick="FlexImport.startSaveRecords();"></span>
				</div>
				<div class="step-notes help prompt">
					<div style="padding-left: 50px;">Notes:</div>
				<ul>
					<li>Invalid records are marked in red. If no specific message is shown, the most likely cause is that the data contains no value for a required field.</li>
					<li>After click 'Save records' it updates the database (irreversible, except by editing the database).</li>
				</ul>
				</div>
			</div>
			<div id="astep4" class="hidden">
				<div class="step-info"></div>
				<div class="step-commands">
					<div id=result-message class="message"></div>
				</div>
			</div>
		</div>

		<p id=info-p></p>

		<p id=col-select-div></p>

		<div id=results></div>
		<p id=records-div-info></p>
		<p id=records-div></p>

		<div id=csv-entry-div>
			<div><textarea id=csv-textarea></textarea></div>
		</div>

	</body>
</html>
